package com.example.shoppingmallclient.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.ServerIp;
import com.example.shoppingmallclient.activity.ShowOrderList;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * @author 袁梦达 2019012364
 */

@SuppressWarnings("all")
public class FragmentForth extends Fragment {

    private String userNumber;
    private String nickName;
    private TextView tvUserNumber;
    private TextView tvNickName;
    private Button btnSeeOrder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.me, container, false);

        tvUserNumber = view.findViewById(R.id.usernumber);
        tvNickName = view.findViewById(R.id.nickname);
        btnSeeOrder = view.findViewById(R.id.btn_see_order);

        new getUserInfo().execute();

        btnSeeOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), ShowOrderList.class);
                startActivity(intent);
            }
        });

        return view;
    }


    private class getUserInfo extends AsyncTask<Object, Object, String>{

        @Override
        protected String doInBackground(Object[] objects) {
            try {
                String serverUrl = "http://" + ServerIp.ServerIp + ":8080/UserInfoServlet";
                URL url = new URL(serverUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }
                return stringBuffer.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String string) {
            String[] strings = string.split(",");
            tvUserNumber.setText("账号：" + strings[0]);
            tvNickName.setText("昵称：" + strings[1]);
        }
    }
}
